﻿using System;
using Jxym.Common.JSON;
using Newtonsoft.Json;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;

namespace Jxym.wlkc2011.Module.Default.SERVER.role
{
    public partial class MenuList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string returnStr = "{List:[],Count:0}";
                try
                {
                    int startIndex = 0, limit = 0, roleId = 0;
                    if (Request.Params["start"] != null) startIndex = Convert.ToInt32(Request.Params["start"]);
                    if (Request.Params["limit"] != null) limit = Convert.ToInt32(Request.Params["limit"]);

                    if (limit != 0)
                    {
                        if (Request.Params["roleId"] != null)
                        {
                            PageBean pb = new PageBean();
                            roleId = Convert.ToInt32(Request.Params["roleId"]);
                            MsSqlDal<User_RolePermissionConfig> iUrpc = new MsSqlDal<User_RolePermissionConfig>();
                            IList<User_RolePermissionConfig> urpcList = iUrpc.GetPageList("RoleID=" + roleId, null, startIndex, limit);
                            DataTable dt = new DataTable();
                            dt.Columns.Add("ID");
                            dt.Columns.Add("MenuID");
                            dt.Columns.Add("MenuName");
                            dt.Columns.Add("IsSelect");
                            dt.Columns.Add("IsAdd");
                            dt.Columns.Add("IsUpdata");
                            dt.Columns.Add("IsDel");
                            foreach (User_RolePermissionConfig info in urpcList)
                            {
                                object[] dr = new object[] { info.ID, info.MenuID, GetMenuName(info.MenuID), GetPermissionValue("S", info.PermissionValue), GetPermissionValue("A", info.PermissionValue), GetPermissionValue("U", info.PermissionValue), GetPermissionValue("D", info.PermissionValue) };
                                dt.Rows.Add(dr);
                            }
                            pb.Count = iUrpc.GetPageCount("RoleID=" + roleId, null);
                            pb.List = dt;
                            returnStr = JsonConvert.SerializeObject(pb);
                        }
                    }
                }
                catch (Exception) { }
                Response.Write(returnStr);
            }
        }

        private string GetMenuName(int menuid)
        {
            MsSqlDal<User_TreeMenu> iUtm = new MsSqlDal<User_TreeMenu>();
            User_TreeMenu utm = iUtm.GetModel("ID=" + menuid, null);
            if (utm != null)
            {
                return utm.Text;
            }
            else
            {
                return "-";
            }
        }

        private string GetPermissionValue(string command, string permissionValue)
        {
            string validation = "false";

            char[] pvs = permissionValue.ToCharArray();

            switch (command)
            {
                default://查
                    if (pvs[0] == '1') validation = "true";
                    break;
                case "A"://增
                    if (pvs[1] == '1') validation = "true";
                    break;
                case "U"://改
                    if (pvs[2] == '1') validation = "true";
                    break;
                case "D"://删
                    if (pvs[3] == '1') validation = "true";
                    break;
            }

            return validation;
        }
    }
}